/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassFrame;

import GamePlay.CanvasGame;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author duynt3   
 */
public class RMS {

    public RecordStore rs;
    public CanvasGame canvas;
    private ByteArrayOutputStream bos;
    private DataOutputStream dos;
    private ByteArrayInputStream bis;
    private DataInputStream dis;
    private byte[] writeByte;
    private static final String RECORD_STORE_NAME = "DHBC_normal";
    private static final int NUM_RECORD = 5;

    public RMS(CanvasGame canvas) {
        this.canvas = canvas;
    }

    public void open() {
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);

            if (rs.getNumRecords() == 0) {
                Player p;
                p = new Player(null, 0, 1, 3000, 0, "Xuân Bắc");
                insert(p);
                p = new Player(null, 0, 1, 2000, 0, "Công Lý");
                insert(p);
                p = new Player(null, 0, 1, 1500, 0, "Tự Long");
                insert(p);
                p = new Player(null, 0, 1, 1200, 0, "Vân Dung");
                insert(p);
                p = new Player(null, 0, 1, 1000, 0, "Văn Hiệp");
                insert(p);
                p = new Player(null, 0, 0, 0, 0, "");
                insert(p);
                
                initLevelMax(0);
            }
        } catch (RecordStoreException re) {
            System.out.println("Can't open RMS");
            System.out.println(re.getMessage());
        }
    }

    public void close() {
        try {
            rs.closeRecordStore();
        } catch (NullPointerException npe) {
            System.out.println("RMS 's reference is null");
            System.out.println(npe.getMessage());
        } catch (RecordStoreException e) {
            System.out.println("Can't close RMS");
            System.out.println(e.getMessage());
        }
    }

    public void initLevelMax(int max){
        try {
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);

            dos.writeInt(max);

            dos.flush();

            writeByte = bos.toByteArray();

            rs.addRecord(writeByte, 0, writeByte.length);
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't wirte data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
    }
    
    public void insert(final Player p) {
        try {
            bos = new ByteArrayOutputStream();
            dos = new DataOutputStream(bos);

            dos.writeUTF(p.getName());
            dos.writeInt(p.getScore());
            dos.writeInt(p.getLevel());
            dos.writeInt(p.getMoney());

            dos.flush();

            writeByte = bos.toByteArray();

            rs.addRecord(writeByte, 0, writeByte.length);
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't wirte data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
    }

    public void replaceWith(final Player p) {
        try {
            for (int id = 1; id <= RMS.NUM_RECORD; ++id) {
                bis = new ByteArrayInputStream(rs.getRecord(id));
                dis = new DataInputStream(bis);

                String name = dis.readUTF();
                int score = dis.readInt();

                if (score < p.getScore()) {
                    byte[] tbye1;

                    for (int i = RMS.NUM_RECORD; i >= id + 1; --i) {
                        tbye1 = rs.getRecord(i - 1);
                        rs.setRecord(i, tbye1, 0, tbye1.length);
                    }
                    bos = new ByteArrayOutputStream();
                    dos = new DataOutputStream(bos);

                    dos.writeUTF(p.getName());
                    dos.writeInt(p.getScore());
                    dos.writeInt(p.getLevel());
                    dos.writeInt(p.getMoney());

                    dos.flush();

                    writeByte = bos.toByteArray();

                    rs.setRecord(id, writeByte, 0, writeByte.length);
                    break;
                }
            }
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't wirte data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
    }

    public int readLevelMax() {
        int levelMax = 0;
        try {
            bis = new ByteArrayInputStream(rs.getRecord(7));
            dis = new DataInputStream(bis);
            levelMax = dis.readInt();
            
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't read data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
        
        return levelMax;
    }
    
    public void saveLevelMax(int level) {

        bos = new ByteArrayOutputStream();
        dos = new DataOutputStream(bos);
        try {
            dos.writeInt(level);
            dos.flush();
        } catch (IOException ex) {
        }
        writeByte = bos.toByteArray();
        try {
            rs.setRecord(7, writeByte, 0, writeByte.length);
        } catch (InvalidRecordIDException in) {
        } catch (RecordStoreNotOpenException re) {
        } catch (RecordStoreException re) {
        }

    }

    
    
    public Vector read() {
        Vector v = new Vector();
        Player p;
        try {
            String name;
            int score;
            int level;
            int miss;

            for (int id = 1; id <= RMS.NUM_RECORD; ++id) {
                bis = new ByteArrayInputStream(rs.getRecord(id));
                dis = new DataInputStream(bis);

                name = dis.readUTF();
                score = dis.readInt();
                level = dis.readInt();
                miss = dis.readInt();

                p = new Player(null, 20, level, score, miss, name);
                v.addElement(p);
                System.out.println("tc" + p.getName() + " " + p.getScore());
            }
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't read data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }
        for (int i = 0; i < v.size(); ++i) {
            Player p1 = (Player) v.elementAt(i);
            System.out.println("pho" + p1.getName() + " " + p1.getScore());
        }
        return v;
    }

    public void save(Player p, int index) {

        bos = new ByteArrayOutputStream();
        dos = new DataOutputStream(bos);
        try {
            dos.writeUTF(p.getName());
            dos.writeInt(p.getScore());
            dos.writeInt(p.getLevel());
            dos.writeInt(p.getMoney());
            dos.flush();
        } catch (IOException ex) {
        }
        writeByte = bos.toByteArray();
        try {
            rs.setRecord(index, writeByte, 0, writeByte.length);
        } catch (InvalidRecordIDException in) {
        } catch (RecordStoreNotOpenException re) {
        } catch (RecordStoreException re) {
        }

    }

    public Player read(int index) {

        Player p = null;
        try {
            String name;
            int score;
            int level;
            int miss;


            bis = new ByteArrayInputStream(rs.getRecord(index));
            dis = new DataInputStream(bis);

            name = dis.readUTF();
            score = dis.readInt();
            level = dis.readInt();
            miss = dis.readInt();

            p = new Player(null, 20, level, score, miss, name);
        } catch (IOException ioe) {
            System.out.println("Can't write data to RMS");
            System.out.println(ioe.getMessage());
        } catch (RecordStoreException rno) {
            System.out.println("Can't read data to RMS becase RMS closing");
            System.out.println(rno.getMessage());
        }

        return p;
    }

    public boolean is_top(final Player p) {
        boolean result = false;

        try {
            bis = new ByteArrayInputStream(rs.getRecord(RMS.NUM_RECORD));
            dis = new DataInputStream(bis);

            String name = dis.readUTF();
            int score = dis.readInt();
            int level = dis.readInt();
            int miss = dis.readInt();

            if (score < p.getScore()) {
                result = true;
            }
        } catch (RecordStoreException rse) {
            System.out.println("in is_top method");
            System.out.println(rse.getMessage());
        } catch (IOException ioe) {
            System.out.println("error with reading data from input stream");
            System.out.println(ioe.getMessage());
        }
        return result;
    }
}
